package com.edt.ichannel.admin.controller;

import cc.edt.iceutils2.image.IceValidateImage;
import cc.edt.iceutils2.random.IceValidateCodeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
@RequestMapping("/")
public class CommonController{
    @Resource
    private HttpSession httpSession;
    @Resource
    private HttpServletResponse httpServletResponse;
    @Resource
    private HttpServletRequest httpServletRequest;


    /**
     * 页面跳转公共controller
     *
     * @author 刘钢 2017年4月12日 下午2:08:26
     */
    @RequestMapping("redirect")
    public String redirect() {
        return httpServletRequest.getParameter("page");
    }

    /**
     * session超时处理页
     *
     * @author 刘钢 2017年4月12日 下午2:09:55
     */
    @RequestMapping("timeout")
    public String timeout() {
        try {
            // 如果是ajax请求响应头会有，x-requested-with
            if (httpServletRequest.getHeader("x-requested-with") != null && httpServletRequest.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
                // 在响应头设置session状态
                httpServletResponse.setHeader("sessionstatus", "timeout");
                httpServletResponse.setContentType("text/html;charset=utf-8");
                String str = "{\"success\":false,\"message\":\"用户登陆超时,请重新登录\"}";
                PrintWriter writer = httpServletResponse.getWriter();
                writer.write(str);
                writer.flush();
                writer.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "timeout";
    }

//    /**
//     * 获取UE后台配置的config.json配置
//     *
//     * @author 刘钢 2017年4月12日 下午2:10:27
//     */
//    @RequestMapping("/getUEConfig")
//    @ResponseBody
//    public void config() {
//        httpServletResponse.setContentType("application/json");
//        String rootPath = httpServletRequest.getSession().getServletContext().getRealPath("/");
//        String exec = new ActionEnter(httpServletRequest, rootPath).exec();
//        WriterToPageByString(exec);
//    }

    /**
     * 跳转到初始化页面
     *
     * @author 奚艺轩 2017-5-4 下午1:43:47
     */
    @RequestMapping("/toInit")
    public String toInit() {
        return "";
    }


    @RequestMapping("ue")
    public String ue() {
        return "ueditor";
    }
    /**
     * 系统登录页
     *
     * @author 刘钢 2017-05-18 11:24
     */
    @RequestMapping("login")
    public String login() {
        return "login";
    }
    /**
     * 验证码生成
     *
     * @author 刘钢 2017-05-18 11:24
     */
    @RequestMapping("getValidateCode")
    @ResponseBody
    public void getValidateCodeAction() {
        // 禁止图像缓存。
        httpServletResponse.setHeader("Pragma", "no-cache");
        httpServletResponse.setHeader("Cache-Control", "no-cache");
        httpServletResponse.setDateHeader("Expires", 0);
        httpServletResponse.setContentType("image/jpeg");
        String code = IceValidateCodeUtils.getValidateCode();
        ServletOutputStream servletOutputStream = null;
        try {
            servletOutputStream = httpServletResponse.getOutputStream();
            BufferedImage buffImg = IceValidateImage.getImageAsBufferedImage(code);
            ImageIO.write(buffImg, "png", servletOutputStream);
            servletOutputStream.flush();
            servletOutputStream.close();
            httpServletResponse.flushBuffer();
            httpSession.setAttribute("VALIDATECODE", code);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
